﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Yugi.Client.WebSerive;
using Yugi.Client.ViewModel;
using Yugi.Client;
using Yugi_Gui.Util_Control;
using Yugi_Gui.COntrol;
using System.Collections;

namespace Yugi_Gui
{
	/// <summary>
	/// Interaction logic for RoomLayout.xaml
	/// </summary>
	public partial class ExternalLayout : UserControl , IInvoke
	{
        MainServerHandler mainserver;
        WebServiceCommunication cm= new WebServiceCommunication();
        ExternalModel model;

        ListUser listonline;
        ChannelControl channel;
		public ExternalLayout()
		{
			this.InitializeComponent();
            this.Loaded += new RoutedEventHandler(RoomLayout_Loaded);
            //this.CreateRoom.OnClick += new Action<object>(create_OnClick);
            this.deck.OnClick += new Action<object>(deck_OnClick);
            this.AutoFind.OnClick += new Action<object>(AutoFind_OnClick);
		}

        void AutoFind_OnClick(object obj)
        {
            try
            {
                int RoomID = this.list_room.GetRoomRandom().ID;
                mainserver.SendMessageToServer(Common.MSG_GSM.L_JoinRoom, RoomID);
            }
            catch(Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }

        void UpdateFriend()
        {
            mainserver.Login_Model.friend_s = new List<PlayerInfo>();
            //model.friend_s = new List<PlayerInfo>();
            Action<Object> EventFriend = (o) =>
            {
                list_friend.listView.Items.Clear();
                List<PlayerInfo> list = o as List<PlayerInfo>;
                foreach (var item in list)
                {
                    mainserver.Login_Model.AddPlayerInfo(item);
                    this.list_friend.AddUser(item);
                }
            };
            cm.UpdateListFriendAsync(this, EventFriend, mainserver.External_Model.UserID);
        }
        void UpdateRoom()
        {
            model.roominfo_s = new List<RoomInfo>();
            Action<Object> EventRoom = (o) =>
            {
                list_room.listView.Items.Clear();
                List<RoomInfo> list = o as List<RoomInfo>;
                foreach (var item in list)
                {
                    model.AddRoomInfo(item);
                    this.list_room.AddRoom(item);
                }
            };
            cm.UpdateListRoomAsync(this, EventRoom);
        }

        void RoomLayout_Loaded(object sender, RoutedEventArgs e)
        {

            mainserver = ((Window)(this.Parent)).DataContext as MainServerHandler;
            model = mainserver.External_Model;
            model.Presenter = this;
            this.list_room.OnChoice += new Action<object>(list_room_OnChoice);
            this.list_friend.OnChoice += new Action<object>(list_user_OnChoice);
            this.list_friend.ContexMenu += new Action<object, object>(list_friend_ContexMenu);
            if (mainserver.Current_Room_ID != -1)
            {
                mainserver.SendMessageToServer(Common.MSG_GSM.L_OutRoom, mainserver.Current_Room_ID);
            }
            if (model.roominfo_s == null)
            {

                cm.SetAuth("yugioh", "admin123");
                UpdateFriend();
                UpdateRoom();
                this.status.Text = "Loading.........";
            }
            else
            {
                foreach (var item in model.roominfo_s)
                {
                    this.list_room.AddRoom(item);
                }
                foreach (var i in mainserver.Login_Model.friend_s)
                {
                    this.list_friend.AddUser(i);
                }
            }

            listonline = new ListUser(mainserver);
            listonline.AddCanvas(LayoutRoot);
            channel = new ChannelControl(mainserver);
            channel.AddCanvas(LayoutRoot, 500, 40);
        }

        void list_friend_ContexMenu(object obj1, object obj2)
        {
            PlayerInfo playerinfo = (PlayerInfo)obj1;
            MenuItem mi = (MenuItem)obj2;
            if (playerinfo == null || mi == null)
                return;
            if (mi.Name == "goto_room")
            {
                mainserver.SendMessageToServer(Common.MSG_GSM.L_JoinRoomF, (int)playerinfo.ID);
            }
            else if (mi.Name == "chat")
            {
                mainserver.ChatManager_Model.Add(playerinfo.NickName, mainserver.Login_Model.userlogin.GetUserName());
            }
        }
        
        void creat_OnAdd(string from, string to)
        {
            ChatControl chatcontrol = new ChatControl(mainserver, false);//them false
            chatcontrol.Add(LayoutRoot);
        }

        void list_user_OnChoice(object obj)
        {
            PlayerInfo tmp = obj as PlayerInfo;
            if (tmp != null)
            {
                mainserver.SendMessageToServer(Common.MSG_GSM.L_JoinRoomF, (int)tmp.ID);
            }
            else
                LayoutManager.LayoutManager.Insitance.ChangeToLayout(LayoutManager.LayoutType.Externa);
        }

        //void create_OnClick(object obj)
        //{
        //    mainserver.SendMessageToServer(Common.MSG_GSM.L_CreateRoom, txt_RoomName.Text);   
        //}

        void list_room_OnChoice(object obj)
        {
            RoomInfo tmp = obj as RoomInfo;
            if (tmp == null)
                LayoutManager.LayoutManager.Insitance.ChangeToLayout(LayoutManager.LayoutType.Externa);
            else
            {
                mainserver.SendMessageToServer(Common.MSG_GSM.L_JoinRoom, tmp.RoomID);
            }
        }
        void deck_OnClick(object obj)
        {
            LayoutManager.LayoutManager.Insitance.ChangeToLayout(LayoutManager.LayoutType.Deck);
        }
        
        #region IInvoke Members

        public void Do(ClientMSG msg, params object[] obj)
        {
            switch(msg)
            {
                case ClientMSG.OutRoom_OK:
                    model.GetRoomInfo(Convert.ToInt32(obj[0])).CountUser = Convert.ToInt32(obj[1]);
                    list_room.GetRoom(Convert.ToInt32(obj[0])).count.Text = (string)(obj[1]);
                    if (mainserver.External_Model.UserID == Convert.ToInt32(obj[2]))
                    {
                        this.mainserver.Current_Room_ID = -1;
                        mainserver.Room_Model.tableinfo_s = null;
                        LayoutManager.LayoutManager.Insitance.ChangeToLayout(LayoutManager.LayoutType.Externa);
                    }
                    break;
                case ClientMSG.CreateRoom_OK:
                    RoomInfo roominfo = new RoomInfo();
                    roominfo.CountUser = Convert.ToInt32(obj[3]);
                    roominfo.RoomID = Convert.ToInt32(obj[0]);
                    roominfo.Level = Convert.ToInt32(obj[2]);
                    roominfo.RoomName = (string)obj[1];
                    model.AddRoomInfo(roominfo);
                    list_room.AddRoom(roominfo);
                    break;
                case ClientMSG.JoinRoom_OK:
                    if(mainserver.External_Model.UserID == Convert.ToInt32(obj[2]))
                    {
                        LayoutManager.LayoutManager.Insitance.ChangeToLayout(LayoutManager.LayoutType.Room);
                        this.mainserver.Current_Room_ID = Convert.ToInt32(obj[0]);
                    }
                    model.GetRoomInfo(Convert.ToInt32(obj[0])).CountUser = Convert.ToInt32(obj[1]);
                    list_room.GetRoom(Convert.ToInt32(obj[0])).count.Text = (string)(obj[1]);
                    break;
                case ClientMSG.Update_Friend_OK:
                    try
                    {
                        mainserver.Login_Model.getfriend(Convert.ToInt32(obj[0])).IsOnline = 1;
                        list_friend.GetUser(Convert.ToInt32(obj[0])).state.Text = "OnLine";
                    }
                    catch { }
                    break;
                case ClientMSG.Update_Friend_Off_OK:
                    try
                    {
                        mainserver.Login_Model.getfriend(Convert.ToInt32(obj[0])).IsOnline = 0;
                        list_friend.GetUser(Convert.ToInt32(obj[0])).state.Text = "OffLine";
                    }
                    catch { }
                    break;
            }
        }

        public void Refresh()
        {
            //if (mainserver.Current_Room_ID != -1)
            //{
            //    mainserver.SendMessageToServer(Common.MSG_GSM.L_OutRoom, mainserver.Current_Room_ID);
            //    mainserver.Current_Room_ID = -1;
            //}
            ////UpdateRoom(null);
        }
        #endregion

        internal void GameLayout_Unloaded()
        {
        }
    }
}